package com.monkey.demolist.tree.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.monkey.demolist.R;
import com.monkey.demolist.tree.util.TreeHelper;
import com.monkey.demolist.tree.bean.Node;

import java.util.List;

/**
 * Description:
 * Author: Archur
 * Date: 2017-10-20
 * Time: 15:37
 */


public class SimpleTreeViewAdapter extends TreeListViewAdapter {

    public SimpleTreeViewAdapter(ListView tree, Context context, List datas, int defaultExpandLevel) throws IllegalAccessException {
        super(tree, context, datas, defaultExpandLevel);
    }

    @Override
    public View getConverView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.adapter_simple_tree_layout,parent,false);
            holder = new ViewHolder();
            holder.mIcon = ((ImageView) convertView.findViewById(R.id.adpter_simple_tree_icon));
            holder.mlable = (TextView) convertView.findViewById(R.id.adpter_simple_tree_label);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (node.getIcon() == -1){
            holder.mIcon.setVisibility(View.INVISIBLE);
        }else {
            holder.mIcon.setVisibility(View.VISIBLE);
            holder.mIcon.setImageResource(node.getIcon());
        }

        holder.mlable.setText(node.getName());

        return convertView;
    }

    public void addExtraNode(int position, String text) {
        Node node = (Node) mVisibleNodes.get(position);
        int indexOf = mAllNodes.indexOf(node);

        Node extraNode = new Node(-1,node.getId(),text);
        extraNode.setParent(node);
        node.getChildren().add(extraNode);

        mAllNodes.add(indexOf+1,extraNode);

        mVisibleNodes = TreeHelper.filterVisibleNodes(mAllNodes);
        notifyDataSetChanged();
    }

    private class ViewHolder{
        ImageView mIcon;
        TextView mlable;
    }
}
